//
//  SideBarViewController.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/3/7.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "SideBarViewController.h"
#import "XXTool.h"

#import "CellForSideBarViewController.h"

@interface SideBarViewController ()<UITableViewDataSource,UITableViewDelegate,UIScrollViewDelegate>

@property (nonatomic) UIImageView *imgContent;

@property (nonatomic) UILabel *userName;

@property (nonatomic) UILabel *userCompany;

@property (nonatomic) UIView *pushInfoVCContent;

@property (nonatomic)  UITableView *tableview;

@property (nonatomic) UIImageView *bottomTipArrow;  //底部提示箭头

@property (nonatomic) UIImageView *topTipArrow;  //顶部提示箭头
@property(nonatomic) BOOL isLoadMeassge;  // 已经加载过消息

@end

@implementation SideBarViewController
{
    NSMutableArray<SideBarCellModel*> *data;
    NSInteger nowIndex;
}

#pragma mark UI
-(UIImageView*)imgContent
{
    if (_imgContent==nil)
    {
        _imgContent = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"默认头像"]];
        _imgContent.contentMode = UIViewContentModeScaleAspectFill;
        _imgContent.clipsToBounds = YES;
        [self.view addSubview:_imgContent];
    }
    return _imgContent;
}

-(UILabel*)userName
{
    if (_userName==nil)
    {
        _userName = [[UILabel alloc] init];
        _userName.textColor = [UIColor whiteColor];
        _userName.font = [XXTool systemFontOfSize:15];
        [self.view addSubview:_userName];
    }
    return _userName;
}

-(UILabel*)userCompany
{
    if (_userCompany==nil)
    {
        _userCompany = [[UILabel alloc] init];
        _userCompany.textColor = [UIColor whiteColor];
        _userCompany.font = [XXTool systemFontOfSize:15];
        [self.view addSubview:_userCompany];
    }
    return _userCompany;
}

-(UIView*)pushInfoVCContent
{
    if (_pushInfoVCContent==nil)
    {
        _pushInfoVCContent = [[UIView alloc] init];
        _pushInfoVCContent.backgroundColor = [UIColor clearColor];
        _pushInfoVCContent.userInteractionEnabled = YES;
        [_pushInfoVCContent addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushToPersonInfoVC)]];
        [self.view addSubview:_pushInfoVCContent];
    }
    return _pushInfoVCContent;
}

-(UITableView*)tableview
{
    if (_tableview==nil)
    {
        _tableview = [[UITableView alloc] init];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor = self.view.backgroundColor;
        [self.view addSubview:_tableview];
    }
    return _tableview;
}

-(UIImageView*)bottomTipArrow
{
    if (_bottomTipArrow==nil)
    {
        _bottomTipArrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"底部提示箭头"]];
        _bottomTipArrow.contentMode = UIViewContentModeScaleAspectFit;
        [self.view addSubview:_bottomTipArrow];
        if (_tableview.contentSize.height<=_tableview.frame.size.height)
        {
            _bottomTipArrow.hidden = YES;
        }
    }
    return _bottomTipArrow;
}

-(UIImageView*)topTipArrow
{
    if (_topTipArrow==nil)
    {
        _topTipArrow = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"顶部提示箭头"]];
        _topTipArrow.contentMode = UIViewContentModeScaleAspectFit;
        [self.view addSubview:_topTipArrow];
        _topTipArrow.hidden = YES;
    }
    return _topTipArrow;
}

#pragma  mark sub
-(void)initParameter
{
    data = [[NSMutableArray alloc] init];
    for (int i=0; i<11; i++)
    {
        SideBarCellModel *temp = [[SideBarCellModel alloc] init];
        temp.messageVaild = YES;
        switch (i)
        {
            case 0:
                temp.imgName = @"首页";
                temp.labelName = NSLocalizedString(@"首页", nil);
                temp.isHiddenLine = YES;
                temp.messageCount = @"";
                [data addObject:temp];
                break;
                
            case 1:
                temp.imgName = @"展会简介";
                temp.labelName = NSLocalizedString(@"展会简介", nil);
                temp.isHiddenLine = YES;
                temp.messageCount = @"";
                [data addObject:temp];
                break;
                
            case 2:
                temp.imgName = @"电子会刊";
                temp.labelName = NSLocalizedString(@"2016电子会刊", nil);
                temp.isHiddenLine = NO;
                temp.messageCount = @"";
                [data addObject:temp];
                break;
                
            case 3:
                temp.imgName = @"商贸配对";
                temp.labelName = NSLocalizedString(@"商贸配对", nil);
                temp.isHiddenLine = YES;
                temp.messageCount = @"";
                [data addObject:temp];
                break;
                
            case 4:
                temp.imgName = @"消息";
                temp.labelName = NSLocalizedString(@"消息", nil);
                temp.isHiddenLine = YES;
                temp.messageCount = @"";
                [data addObject:temp];
                break;
                
            case 5:
                temp.imgName = @"收藏夹";
                temp.labelName = NSLocalizedString(@"收藏夹", nil);
                temp.isHiddenLine = YES;
                temp.messageCount = @"";
                [data addObject:temp];
                break;
                
            case 6:
                temp.imgName = @"展会地图";
                temp.labelName = NSLocalizedString(@"展会地图", nil);
                temp.isHiddenLine = NO;
                temp.messageCount = @"";
                [data addObject:temp];
                break;
                
            case 7:
                temp.imgName = @"展位预订";
                temp.labelName = NSLocalizedString(@"展位预订", nil);
                temp.isHiddenLine = YES;
                temp.messageCount = @"";
                [data addObject:temp];
                break;
                
            case 8:
                temp.imgName = @"观众预登记";
                temp.labelName = NSLocalizedString(@"观众预登记", nil);
                temp.isHiddenLine = YES;
                temp.messageCount = @"";
                [data addObject:temp];
                break;
                
            case 9:
                temp.imgName = @"联系我们";
                temp.labelName = NSLocalizedString(@"联系我们", nil);
                temp.isHiddenLine = YES;
                temp.messageCount = @"";
                [data addObject:temp];
                break;
                
            case 10:
                
                temp.imgName = @"扫描二维码";
                temp.labelName = NSLocalizedString(@"扫描二维码", nil);
                temp.isHiddenLine = YES;
                temp.messageCount = @"";
                [data addObject:temp];
                break;

            default:
                break;
        }
    }
    
    nowIndex = 0;
}

-(void)initUI
{
    self.view.backgroundColor = [UIColor colorWithRed:38/255.0 green:38/255.0 blue:38/255.0 alpha:1.0];
    [self imgContent];
    [self userName];
    [self userCompany];
    [self pushInfoVCContent];
    [self tableview];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushToPersonInfoVC) name:@"sumbitSuccess" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(successIntro:) name:@"successLogin" object:nil];
   
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(firstIntro:) name:@"successLoginAndFirstIntro" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didUserInfoSetup) name:@"DidSuccessFullSetupUserInfo" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exhibitorSuccessSupply:) name:@"exhibitorSuccess" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exhibitorSuccessInfo:) name:@"exhibitorSuccessInfo" object:nil];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(leftShowYes) name:@"leftShowYes" object:nil];
}

-(void)leftShowYes
{
    //if(![self isLoadMeassge]){
        [DKHTTPRequest sendDataWithShortUrl:@"msg_getmsginfo"
                                      Param:[XXTool formatStringWithLanguage:[NSString stringWithFormat:@"userid=%@&passwd=%@",[UserInfo shareUserInfo].userID,[UserInfo shareUserInfo].userPassword]]
                                 ImageArray:nil
                                    TimeOut:10
                                 Completion:^BOOL(NSMutableDictionary *serverDic, DKHTTPResponseType bufferType) {
                                     if ([[serverDic objectForKey:@"code"] intValue]==200)
                                     {
                                         self.isLoadMeassge=true;
                                         SideBarCellModel *temp = [data objectAtIndex:4];
                                         if ([[serverDic objectForKey:@"value"] intValue]==1)
                                         {
                                             temp.messageVaild = NO;
                                             temp.messageCount = [NSString stringWithFormat:@"%@",[serverDic objectForKey:@"value2"]];
                                         }
                                         else
                                         {
                                             temp.messageVaild = YES;
                                             temp.messageCount = @"";
                                         }
                                         [_tableview reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:4 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
                                     }
                                     return YES;
                                 } IsBuffering:NO
                          IsNeedReplaceWrap:NO
                            IsUseUpLoadHead:NO];
   // }
    
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)refreshLayout:(CGRect)frame
{
    frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height);
    _imgContent.frame = CGRectMake(27
                                   , 60
                                   , 60, 60);
    _imgContent.layer.cornerRadius = _imgContent.frame.size.width/2;
    _userName.frame = CGRectMake(_imgContent.frame.origin.x+_imgContent.frame.size.width+15
                                 , _imgContent.frame.origin.y+10
                                 , frame.size.width-(_imgContent.frame.origin.x*2+_imgContent.frame.size.width+15)
                                 , 17);
    _userCompany.frame = CGRectMake(_userName.frame.origin.x
                                    , _userName.frame.origin.y+_userName.frame.size.height+6
                                    , _userName.frame.size.width
                                    , _userName.frame.size.height);
    _pushInfoVCContent.frame = CGRectMake(_imgContent.frame.origin.x
                                          , _imgContent.frame.origin.y
                                          , _userCompany.frame.origin.x-_imgContent.frame.origin.x+_userCompany.frame.size.width-50
                                          , _imgContent.frame.size.height);
    
    self.topTipArrow.frame = CGRectMake(0
                                    , _pushInfoVCContent.frame.size.height+_pushInfoVCContent.frame.origin.y+7
                                    , frame.size.width-50
                                    , 7);
    
    _tableview.frame = CGRectMake(0
                                  , _imgContent.frame.origin.y+_imgContent.frame.size.height+20
                                  , frame.size.width
                                  , frame.size.height-(_imgContent.frame.size.height+_imgContent.frame.origin.y+20+40));
    self.bottomTipArrow.frame = CGRectMake(0
                                           , _tableview.frame.origin.y+_tableview.frame.size.height+7
                                           , _topTipArrow.frame.size.width
                                           , _topTipArrow.frame.size.height);
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self didUserInfoSetup];
}

-(void)didUserInfoSetup
{
    _userName.text = [UserInfo shareUserInfo].userName;
    _userCompany.text = [UserInfo shareUserInfo].userCompany;
    [_imgContent setImageWithUrl:[UserInfo shareUserInfo].userHeadImgUrl placerHolder:[UIImage imageNamed:@"默认头像"]];
}

#pragma mark tableviewDataSource
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *reuseIdentifier = @"CellForSideBarViewController";
    CellForSideBarViewController *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (cell==nil)
    {
        cell = [[CellForSideBarViewController alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        UIView *bgcellView = [[UIView alloc] initWithFrame:cell.contentView.frame];
        bgcellView.backgroundColor = [UIColor colorWithRed:62/255.0 green:62/255.0 blue:62/255.0 alpha:1.0];
        cell.selectedBackgroundView = bgcellView;
    }
    [cell setInfo:[[NSDictionary dictionaryWithObject:data[indexPath.row] forKey:CellForSideBarViewControllerKey] mutableCopy]];
    return cell;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return data.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

#pragma mark tableviewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([_delegate respondsToSelector:@selector(sideBarView:didClickIndex:clickThisBefore:isNeedSpecialTreat:)])
    {
        if([_delegate sideBarView:self didClickIndex:indexPath.row clickThisBefore:nowIndex isNeedSpecialTreat:NO])
        {
            [self.sideViewController showMainView];
            nowIndex=indexPath.row;
        }
        else
        {
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        }
    }
    // 判断点击的是否为 消息
    if(indexPath.row == 4){
         SideBarCellModel *temp = [data objectAtIndex:4];
        temp.messageVaild = YES;
        temp.messageCount = @"";
        [_tableview reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:4 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
    }
    
    
}

#pragma mark scrollviewdelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat gapHeigth = scrollView.contentSize.height-scrollView.frame.size.height;
    if (gapHeigth<=0)
    {
        return;
    }
    if (scrollView.contentOffset.y>0)
    {
        _topTipArrow.hidden = NO;
    }
    else
    {
        _topTipArrow.hidden = YES;
    }
    
    if (scrollView.contentOffset.y<gapHeigth)
    {
        _bottomTipArrow.hidden = NO;
    }
    else
    {
        _bottomTipArrow.hidden =YES;
    }
}

/**
 *  观众登录成功
 */
-(void)pushToPersonInfoVC
{
    if ([_delegate respondsToSelector:@selector(sideBarView:didClickIndex:clickThisBefore:isNeedSpecialTreat:)])
    {
        if([_delegate sideBarView:self didClickIndex:11 clickThisBefore:nowIndex isNeedSpecialTreat:NO])
        {
            [self.sideViewController showMainView];
            nowIndex = 11;
        }
    }
}

/**
 *  已登陆并填写了资料信息
 */
-(void)successIntro:(NSNotification*) notification
{
//    if ([_delegate respondsToSelector:@selector(sideBarView:didClickIndex:clickThisBefore:isNeedSpecialTreat:)])
//    {
//        if ([_delegate sideBarView:self didClickIndex:0 clickThisBefore:nowIndex isNeedSpecialTreat:NO]) {
//            [self.sideViewController showMainView];
//            nowIndex = 0;
//        }
//    }
}

/**
 *  跳过或者观众登录成功或者展商登录并完善资料
 */
-(void)firstIntro:(NSNotification *)notification
{
    BOOL isNeed = [[notification.userInfo objectForKey:@"is"] boolValue];
    if (!isNeed) //需要
    {
        if ([_delegate respondsToSelector:@selector(sideBarView:didClickIndex:clickThisBefore:isNeedSpecialTreat:)])
        {
            if ([_delegate sideBarView:self didClickIndex:1 clickThisBefore:nowIndex isNeedSpecialTreat:NO]) {
                [self.sideViewController showMainView];
                nowIndex = 1;
            }
        }
    }
}

/**
 *  登陆后直接进资料页面
 */
-(void)exhibitorSuccessInfo:(NSNotification*) notification
{
    BOOL isNeed = [[notification.userInfo objectForKey:@"is"] boolValue];
    if (!isNeed)
    {
        if ([_delegate respondsToSelector:@selector(sideBarView:didClickIndex:clickThisBefore:isNeedSpecialTreat:)])
        {
            if ([_delegate sideBarView:self didClickIndex:11 clickThisBefore:nowIndex isNeedSpecialTreat:YES]) {
                [self.sideViewController showMainView];
                nowIndex = 11;
            }
        }
    }
}

/**
 *  展商成功完善资料
 */
-(void)exhibitorSuccessSupply:(NSNotification*)notification
{
    BOOL isNeed = [[notification.userInfo objectForKey:@"is"] boolValue];
    if (!isNeed)
    {
        if ([_delegate respondsToSelector:@selector(sideBarView:didClickIndex:clickThisBefore:isNeedSpecialTreat:)])
        {
            if ([_delegate sideBarView:self didClickIndex:3 clickThisBefore:nowIndex isNeedSpecialTreat:YES]) {
                [self.sideViewController showMainView];
                nowIndex = 3;
            }
        }
    }
}

@end
